package com.android.aircondition.view;


import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.android.aircondition.R;

public class ModeSongfengView extends RelativeLayout{

	private ImageView leafImv;
	private ImageView windyImv;
	private ImageView windyImv1;
	private ImageView windyImv2;
	private ImageView windyImv3;
//	private ImageView windyImv4;
	
	
	public ModeSongfengView(Context context) {
		super(context,null);
		initData(context);
	}

	public ModeSongfengView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initData(context);
		
	}

	private void initData(Context context){
		
		leafImv   = new ImageView(context); leafImv  .setBackgroundResource(R.drawable.leaf);
		windyImv  = new ImageView(context); windyImv .setBackgroundResource(R.drawable.windy);
		windyImv1 = new ImageView(context);	windyImv1.setBackgroundResource(R.drawable.windy_1);
		windyImv2 = new ImageView(context); windyImv2.setBackgroundResource(R.drawable.windy_1);
		windyImv3 = new ImageView(context); windyImv3.setBackgroundResource(R.drawable.windy_3);
//		windyImv4 = new ImageView(context); windyImv4.setBackgroundResource(R.drawable.windy_4);
		
		leafImv.setVisibility(View.INVISIBLE);
		windyImv1.setVisibility(View.INVISIBLE);
		windyImv2.setVisibility(View.INVISIBLE);
		windyImv3.setVisibility(View.INVISIBLE);
//		windyImv4.setVisibility(View.INVISIBLE);
		
		LayoutParams leafParam  	 = new LayoutParams(100,102);
		LayoutParams windyImvParam   = new LayoutParams(400,410);
		LayoutParams windyImv1Param  = new LayoutParams(400,410);
		LayoutParams windyImv2Param  = new LayoutParams(400,410);
		LayoutParams windyImv3Param  = new LayoutParams(400,410);
//		LayoutParams windyImv4Param  = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		
		leafParam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		windyImvParam .addRule(RelativeLayout.CENTER_IN_PARENT);
		windyImv1Param.addRule(RelativeLayout.CENTER_IN_PARENT);
		windyImv2Param.addRule(RelativeLayout.CENTER_IN_PARENT);
		windyImv3Param.addRule(RelativeLayout.CENTER_IN_PARENT);
//		windyImv4Param.addRule(RelativeLayout.CENTER_IN_PARENT);
		
		leafImv.setLayoutParams(leafParam);
		windyImv .setLayoutParams(windyImvParam);
		windyImv1.setLayoutParams(windyImv1Param);
		windyImv2.setLayoutParams(windyImv2Param);
		windyImv3.setLayoutParams(windyImv3Param);
//		windyImv4.setLayoutParams(windyImv4Param);
		
		
		this.addView(leafImv);
		this.addView(windyImv);
		this.addView(windyImv1);
		this.addView(windyImv2);
		this.addView(windyImv3);
//		this.addView(windyImv4);
	}
	
	public void startSongfengAnimation(){

		DisplayMetrics dm = new DisplayMetrics();  
		dm = this.getResources().getDisplayMetrics();  
		// 获得屏幕宽度  
		int screenWidth = dm.widthPixels;  
		// 获得屏幕高度  
		int screenHeight = dm.heightPixels; 
		
		
		Animation leafTransferAnimation = new TranslateAnimation(0, -screenWidth,0,screenHeight );
		Animation leafAnimation = new RotateAnimation(0, 720);
		
		Animation windyImvTransferAnimation  = new TranslateAnimation(screenWidth, 0,-screenHeight/2,0);
		Animation windyImv1TransferAnimation = new TranslateAnimation(screenWidth, -screenWidth,-screenHeight/2,screenHeight/2 );
		Animation windyImv2TransferAnimation = new TranslateAnimation(screenWidth, -screenWidth,-screenHeight/2,screenHeight/2 );
		Animation windyImv3TransferAnimation = new TranslateAnimation(screenWidth, -screenWidth,-screenHeight/2,screenHeight/2 );
//		Animation windyImv4TransferAnimation = new TranslateAnimation(screenWidth, -screenWidth,-screenHeight/2,screenHeight/2 );
		
		windyImv.setAnimation(windyImvTransferAnimation);
		windyImv1.setAnimation(windyImv1TransferAnimation);
		windyImv2.setAnimation(windyImv2TransferAnimation);
		windyImv3.setAnimation(windyImv3TransferAnimation);
//		windyImv4.setAnimation(windyImv4TransferAnimation);
		
		AnimationSet leafSet    = new AnimationSet(true);
		leafSet.setInterpolator(new LinearInterpolator());
		leafTransferAnimation.setDuration(2000);
		leafAnimation.setDuration(2000);
		leafSet.addAnimation(leafAnimation);
		leafSet.addAnimation(leafTransferAnimation);
		leafImv.setAnimation(leafSet);
		leafSet.start();
		
		windyImvTransferAnimation.setDuration(1000);
		windyImv1TransferAnimation.setDuration(1000);
		windyImv2TransferAnimation.setDuration(900);
		windyImv3TransferAnimation.setDuration(800);
//		windyImv4TransferAnimation.setDuration(1000);
		
		AnimationSet windySet = new AnimationSet(true);
		windySet.setInterpolator(new LinearInterpolator());
		windySet.addAnimation(windyImv1TransferAnimation);
		windySet.addAnimation(windyImv2TransferAnimation);
		windySet.addAnimation(windyImv3TransferAnimation);
//		windySet.addAnimation(windyImv4TransferAnimation);
		
		windyImv1TransferAnimation.setStartOffset(0);
		windyImv2TransferAnimation.setStartOffset(1000);
		windyImv3TransferAnimation.setStartOffset(1900);
//		windyImv4TransferAnimation.setStartOffset(3000);
		windyImvTransferAnimation.setStartOffset(2700);
		
		windySet.start();
		
	}
	
}
